package slidingwindow;
//水果成蓝
public class TotalFruit {
    public static void main(String[] args) {
        int[] fruits = {3,3,3,1,2,1,1,2,3,3,4};
        System.out.println(totalFruit(fruits));
    }
    public static int totalFruit(int[] fruits) {
        int n = fruits.length;
        int[] hash = new int[n];
        int ret = 0;
        for (int left = 0, right = 0, kinds = 0; right < n; right++) {
            int in = fruits[right];
            if(hash[in]++ == 0) kinds++;
            while(kinds > 2) {
                int out = fruits[left];
                hash[out]--;
                if(hash[out] == 0) kinds--;
                left++;
            }
            ret = Math.max(ret,right-left+1);
        }
        return ret;
    }











//    public static int totalFruit(int[] fruits) {
//        int n = fruits.length;
//        int[] hash = new int[n + 1];
//        int ret = 0;
//        for (int left = 0,kinds = 0, right = 0; right < n; right++) {
//            int in = fruits[right];
//            if (hash[in] == 0) kinds++;
//            hash[in]++;
//            while(kinds > 2) {
//                int out = fruits[left];
//                hash[out]--;
//                if(hash[out] == 0) kinds--;
//               left++;
//            }
//            ret = Math.max(right-left+1,ret);
//        }
//         return ret;
//    }
}
